#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.db import models
import datetime

class Group(models.Model):
    """Definition of Group in Group System

    Necessary Fields are:
        name --> limit 10 characters
        create_datetime --> when create a group, assign it with datetime.datetime.now
        member_nickname --> limit 10 characters
        members --> who create the group is the first member
        administrators --> who create the group is the first administrator
        logo --> imagefield
        description --> textfield, no limit
	join_type --> the limit to join this group

    Optional Fields are:
        domainname_suffix --> limit 30 characters
        friend_groups --> a group can have no friend_groups
    """
    
    name = models.CharField(max_length = 10, unique = True)
    create_datetime = models.DateTimeField(default = datetime.datetime.now)
    member_nickname = models.CharField(max_length = 10, blank=True, null=True)
    members = models.ManyToManyField('SNS.User', related_name = 'groups',
                                     through = 'MemberShip')
    administrators = models.ManyToManyField('SNS.User', related_name = 'admin_groups')
    logo = models.ImageField(upload_to = 'user_image/%Y/%m/%d', max_length = 120, null = True, blank = True)
    description = models.TextField()
    join_type = models.CharField(max_length = 1)

    domainname_suffix = models.CharField(max_length = 30, null = True, blank = True)
    friend_groups = models.ManyToManyField('self', null = True, blank = True)
    friend_group_apply_to = models.ManyToManyField('self', symmetrical = False,
                                                   through = 'FriendGroupApply', related_name = 'friend_group_apply_from')
    
    def __unicode__(self):
        return self.name

    #def toText(self):
        #return 


class MemberShip(models.Model):
    """Definition of Group MemberShip intermediate table

    Necessary Fields are:
        member --> ForeignKey
        group --> ForeignKey
        join_datetime --> when create membership, assign it with datetime.datetime.now
        forbidden --> when create membership ,assign it with False
        credit --> when create membership, assign it with 0
        
    """

    class Meta:
        unique_together = ('member', 'group')
        ordering = ['-join_datetime']
    
    member = models.ForeignKey('SNS.User')
    group = models.ForeignKey('Group')
    join_datetime = models.DateTimeField(default = datetime.datetime.now)
    forbidden = models.BooleanField(default = False)
    credit = models.IntegerField(default = 0)

    def __unicode__(self):
        return self.member.name + u'@' + self.group.name

class FriendGroupApply(models.Model):
    """Definition of Group FriendGroupApply intermediate table

    Necessary Fields are:
        group_from --> ForeignKey
        group_to --> ForeignKey
        apply_datetime --> when create friendgroupapply, assign it with datetime.datetime.now

    """

    class Meta:
        ordering = ['-apply_datetime']
        unique_together = ('group_from', 'group_to')

    group_from = models.ForeignKey('Group', related_name = 'friendgroupapply_to')
    group_to = models.ForeignKey('Group', related_name = 'friendgroupapply_from')
    apply_datetime = models.DateTimeField(default = datetime.datetime.now)

    def __unicode__(self):
        return self.group_from.name + u'-->' + self.group_to.name

class MemberApply(models.Model):
    """Definition of Group MenberApply intermediate table
	
	Necessary Fields are:
            member --> ForeignKey
            group --> ForeignKey
            apply_datetime --> when create memberapply, assign it with datetime.datetime.now

    """
    class Meta:
        ordering = ['-apply_datetime']
        unique_together = ('member', 'group')

    member = models.ForeignKey('SNS.User')
    group = models.ForeignKey('Group')
    apply_datetime = models.DateTimeField(default = datetime.datetime.now)

    def __unicode__(self):
        return self.member.name + u'-->' + self.group.name
    

class Post(models.Model):
    """Definition of Group Post in Group System

    Necessary Fields are:
        title --> limit 100 characters
        content --> no limit
        group --> ForeignKey
        poster --> ForeignKey
        post_datetime --> when post post, assign it with datetime.datetime.now
        min_available_floor_num --> integer
        view_count --> when create a question, assign it with 0
        elite --> Boolean
        stick --> Boolean
        
    """

    class Meta:
        ordering = ['-post_datetime']

    title = models.CharField(max_length = 100)
    attachment = models.CharField(max_length = 100)
    content = models.TextField()
    group = models.ForeignKey('Group')
    poster = models.ForeignKey('SNS.User')
    post_datetime = models.DateTimeField(default = datetime.datetime.now)
    min_available_floor_num = models.IntegerField(default = 1)
    view_count = models.IntegerField(default = 0)
    replied_count = models.IntegerField(default = 0)
    elite = models.BooleanField(default = False)
    stick = models.BooleanField(default = False)

    def __unicode__(self):
        return self.title

class Reply(models.Model):
    """Definition of Group Reply in Group System
    
    Necessary Fields are:
        post --> ForeignKey
        content --> no limit
        replier --> ForeignKey
        floor_num --> integer 
        reply_datetime --> when post post, assign it with datetime.datetime.now
        
    """
    class Meta:
        ordering = ['-reply_datetime']
        
    post = models.ForeignKey('Post')
    content = models.TextField()
    replier = models.ForeignKey('SNS.User')
    floor_num = models.IntegerField()
    reply_datetime = models.DateTimeField(default = datetime.datetime.now)

    def __unicode__(self):
        return self.post.title
        

class Attachment(models.Model):
    """Definition of Attachment in Group System

    Necessary Fields are:
        location --> FileField upload to group_attachment dir,
                     filename attribute can reserve attachment name
    
    """

    name = models.CharField(max_length = 60)
    location = models.FileField(upload_to = 'group_attachment/%Y/%m/%d', max_length = 120)

    def __unicode__(self):
        return self.name
"""
from django import forms
class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    myfile = forms.FileField()
    """
    
